package com.alit.ip.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class RequestUtil {
  /**
   * 返回 HttpServletRequest中的IP地址
   * 
   * @param request
   * @return
   */
  public static String getIp(HttpServletRequest request) {
    String ipAddress = request.getHeader("X-Forwarded-For");
    if (isEmpty(ipAddress)) {
      ipAddress = request.getRemoteAddr();
      if (ipAddress.equals("127.0.0.1")) {
        ipAddress = request.getHeader("X-Real-IP");
      }
    } else {
      ipAddress = ipAddress.split(",")[0];
    }
    return ipAddress;
  }

  public static Map<String, String> getRequestIp(HttpServletRequest request) {
    String remoteAddr = request.getRemoteAddr();
    String xRealIP = request.getHeader("X-Real-IP");
    String xForwardedFor = request.getHeader("X-Forwarded-For");
    Map<String, String> map = new HashMap<>();
    map.put("remoteAddr", remoteAddr);
    map.put("xRealIP", xRealIP);
    map.put("xForwardedFor", xForwardedFor);
    return map;
  }

  private static boolean isEmpty(String str) {
    return str == null || "".equals(str);
  }

  /**
   * output key and value of reuest package
   * 
   * @param request
   */
  public static Map<String, String[]> paramaterToString(HttpServletRequest request) {
    Map<String, String[]> parameterMap = request.getParameterMap();
    for (Map.Entry<String, String[]> e : parameterMap.entrySet()) {
      System.out.println(e.getKey());
      System.out.println(e.getValue());
    }
    return parameterMap;
  }

  /**
   * 获取request中的字符串
   * 
   * @param request
   * @return
   * @throws IOException
   */
  public static String getRequestString(HttpServletRequest request) throws IOException {
    String method = request.getMethod();
    // GET
    if (method.equals("GET")) {
      String retval = getGetString(request);
      return retval;
    }
    // POST
    if (method.equals("POST")) {
      String retval = getPostString(request);
      return retval;
    }

    return null;
  }

  /**
   * 获取Get请求中字符串
   * 
   * @param request
   * @return
   */
  private static String getGetString(HttpServletRequest request) {
    String queryString = request.getQueryString();
    if (queryString == null) {
      return null;
    }
    try {
      queryString = new String(queryString.getBytes("ISO-8859-1"), "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    queryString = queryString.replaceAll("%22", "\"");
    return queryString;
  }

  /**
   * 获取POST请求中字符串
   * 
   * @param request
   * @return
   * @throws IOException
   */
  private static String getPostString(HttpServletRequest request) throws IOException {
    byte[] buf = getRequestPostBytes(request);
    String encoding = request.getCharacterEncoding();
    if (encoding == null) {
      encoding = "UTF-8";
    }
    String retval = new String(buf, encoding);
    return retval;
  }

  /**
   * 获取Post请求中的数据流
   * 
   * @param request
   * @return
   * @throws IOException
   */
  private static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException {
    int contentLen = request.getContentLength();
    byte[] buf = new byte[contentLen];
    for (int i = 0; i < contentLen;) {
      ServletInputStream ins = request.getInputStream();
      int readLen = ins.read(buf, i, contentLen - i);
      if (readLen == -1) {
        break;
      }
      i += readLen;
    }
    return buf;
  }
}
